package collector.httpd.metad.report;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.apexcns.core.util.XmlBindUtil;
import collector.core.CollectConstants;
import collector.core.ConfigTask;
import collector.core.config.Config;
import collector.core.util.DateUtil;
import collector.httpd.HttpdUtil;
import collector.metaexec.MetaExec;
import collector.task.JobTaskVO;

import org.eclipse.jetty.server.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ReportHandler {
	private static Logger _log = LoggerFactory.getLogger(ReportHandler.class);
	@SuppressWarnings("unused")
	private static final String _LogPrefixStr = CollectConstants.LOGGER_MODULE_NAME_SMC + CollectConstants.LOGGER_MODULE_NAME_AFTFIX;
	
	@SuppressWarnings("unused")
	private Config config = Config.newInstance();
	@SuppressWarnings("unused")
	private ConfigTask configTask = ConfigTask.newInstance();
	
	public void handle(String target, Request baseReq
			, HttpServletRequest request, HttpServletResponse response, List<MetaExec> execList)
	throws IOException, ServletException {
		baseReq.setQueryEncoding("utf-8");
		baseReq.setHandled(true);

		ReportRequest reqvo = new ReportRequest();
		HttpdUtil.bindRequestBean(reqvo, request);
		//logger.debug(reportRequest.getData());
		doit(target, baseReq, request, response, execList, reqvo);
	}
	
	private void doit(String target, Request baseReq
			, HttpServletRequest request, HttpServletResponse response
			, List<MetaExec> execList, final ReportRequest reqvo)
	throws IOException, ServletException {
		final Object[] vo = new Object[1];

		vo[0] = getMonTaskObject(reqvo);
		bindServerInfo(baseReq, reqvo);

		if(execList == null) return;
		for(final MetaExec handler : execList) {
			if(vo[0]==null) {
			    _log.warn("vo[0] is null");
				return;
			}
			try {
				new Thread() {
					public void run() {
						try {
							handler.execMeta(reqvo, vo[0]);
						} catch (Exception e) {
						    _log.error(e.getMessage(), e);
						}
					}
				}.start();
			} catch(Exception e) {
			    _log.error(e.getMessage());
			}
		}
	}

	// 제거
	private Object getMonTaskObject(ReportRequest reqvo) {
		Object vo = null;
		
		if("TASKUTIL".equals(reqvo.getMonTaskId())) {
			try {
				vo = XmlBindUtil.fromXml(JobTaskVO.class, reqvo.getData());
			} catch (Exception e) {
			    _log.warn(e.getMessage(), e);
			}
		} else if("SYSUTIL".equals(reqvo.getMonTaskId())) {
			try {
				vo = XmlBindUtil.fromXml(JobTaskVO.class, reqvo.getData());
			} catch (Exception e) {
			    _log.warn(e.getMessage(), e);
			}
		} else if("LOGUTIL".equals(reqvo.getMonTaskId())) {
			try {
				vo = XmlBindUtil.fromXml(JobTaskVO.class, reqvo.getData());
			} catch (Exception e) {
			    _log.warn(e.getMessage(), e);
			}
		}
		return vo;
	}
	
	private void bindServerInfo(Request baseReq, ReportRequest reqvo) {
		reqvo.setServerHostName(baseReq.getRemoteHost());
		reqvo.setServerIpAddr(baseReq.getRemoteAddr());
		reqvo.setServerTime(DateUtil.getTimelong());

//		NodeIdVo nodeIdVo = nodeIdService.selectNodeId(reqvo);
//		if(nodeIdVo==null || StringUtil.isEmpty(nodeIdVo.getNodeId())) {
//			return;
//		}
//		reqvo.setServerNodeId(nodeIdVo.getNodeId());
//		
//		// Modify By Jeong - 2012.07.23 ( update for UNKNOWN )
//		//reqvo.setMonCluster(StringUtil.nvl(nodeIdVo.getClusId(),"UNKNOWN"));
//		reqvo.setMonCluster(StringUtil.nvl(reqvo.getMonCluster(), StringUtil.nvl(nodeIdVo.getClusId(),"UNKNOWN")));
	}
	
}
